﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using elementManagement;
using userManagement;
using helpers;
using userInteractveControllers;
using System.Collections.Generic;

namespace WebInterface
{
    public partial class GameGUI : System.Web.UI.Page
    {

        UsersDataPersister users = UsersDataPersister.getInstance();

        protected void Page_Load(object sender, EventArgs e)
        {
            btnCompose.Attributes.Add("style", "display:none");
            lblCredit.Text = "Punguta cu 2 bani: " + Convert.ToInt32(Accessors.getCurrentUser().getCredit()).ToString();
        }

        private void Repopulate()
        {
            lbInventory.DataBind();
            lbOnSale.DataBind();
            lbMarket.DataBind();
            lbPopulation.DataBind();
            lblCredit.Text = "Punguta cu 2 bani: " + Convert.ToInt32(Accessors.getCurrentUser().getCredit()).ToString();

        }

        protected void btnBuyAtom_Click(object sender, EventArgs e)
        {
            GameController gameController = new GameController(Accessors.getCurrentUser());
            gameController.buyAtom();
            Repopulate();
        }

        private List<string> getSelectedItems(CheckBoxList chkList)
        {
            List<string> valueList = new List<string>();
            foreach (ListItem li in chkList.Items)
            {
                if (li.Selected)
                    valueList.Add(li.Value);
            }
            return valueList;
        }

        protected void btnSell_Click(object sender, EventArgs e)
        {
            GameController gameController = new GameController(Accessors.getCurrentUser());
            List<string> guidList = getSelectedItems(lbInventory);
            if (guidList.Count > 0)
            {
                gameController.sellElements(guidList);
                Repopulate();

            }
            else
            {
               // JSMessageBox.ShowMessage(this, "Nici un element selectat");
                MessageBox.Show("Nici un element selectat");    
            }

        }

        protected void btnWithdrawElement_Click(object sender, EventArgs e)
        {
            GameController gameController = new GameController(Accessors.getCurrentUser());
            List<string> guidList = getSelectedItems(lbOnSale);
            if (guidList.Count > 0)
            {
                gameController.withdrawElement(guidList);
                Repopulate();

            }
            else
            {
                //JSMessageBox.ShowMessage(this, "Nici un element selectat");
                MessageBox.Show("Nici un element selectat");

            }

        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Repopulate();

        }

        protected void btnCompose_Click(object sender, EventArgs e)
        {
            GameController gameController = new GameController(Accessors.getCurrentUser());
            List<string> guidList = getSelectedItems(lbInventory);
            if (guidList.Count > 0)
            {
                string NumeCompunere = nume_compunere.Value;
                bool bComposed = gameController.compose(guidList, NumeCompunere);
                lbOnSale.DataBind();
                lbInventory.DataBind();
                if (!bComposed)
                {
                    MessageBox.Show("Forta nu a fost cu tine: Compunerea nu a putut fi efectuata. ");
                }
            }
            else
            {
                // JSMessageBox.ShowMessage(this, "Nici un element selectat");
                MessageBox.Show("Nici un element selectat");
            }

        }

        protected void btnBuyElement_Click(object sender, EventArgs e)
        {
            GameController gameController = new GameController(Accessors.getCurrentUser());
            List<string> guidList = getSelectedItems(lbMarket);
            if (guidList.Count > 0)
            {
                gameController.buy(guidList);
                Repopulate();

            }
            else
            {
                //JSMessageBox.ShowMessage(this, "Nici un element selectat");
                MessageBox.Show("Nici un element selectat");

            }
        }
    }
}
